using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EveApiLib;
using EveSkillPocket.Properties;

namespace EveSkillPocket
{
    public partial class fAccount : Form
    {
        private EveAPIConfig eaConfig;
        private TreeNode tnActivatedChar;

        public fAccount()
        {
            InitializeComponent();
        }

        public fAccount(EveAPIConfig conf)
        {
            this.eaConfig = conf;
            this.InitializeComponent();
            this.InitImageList();
            this.UpdateCharacterTree();
        }

        public void ActivateCharacter(TreeNode tn)
        {
            if (this.tnActivatedChar == null)
            {
                this.tnActivatedChar = tn;
                this.tnActivatedChar.ImageIndex = 0;
                this.tnActivatedChar.Parent.ImageIndex = 0;
                this.tnActivatedChar.SelectedImageIndex = 0;
                this.tnActivatedChar.Parent.SelectedImageIndex = 0;
                this.eaConfig.ActivatedChar = (EveCharacter) tn.Tag;
                this.eaConfig.ActivatedAccount = (EveAccount) tn.Parent.Tag;
            }
            else if (!this.tnActivatedChar.Equals(tn))
            {
                this.tnActivatedChar.ImageIndex = 1;
                this.tnActivatedChar.Parent.ImageIndex = 1;
                this.tnActivatedChar.SelectedImageIndex = 1;
                this.tnActivatedChar.Parent.SelectedImageIndex = 1;
                tn.ImageIndex = 0;
                tn.Parent.ImageIndex = 0;
                tn.SelectedImageIndex = 0;
                tn.Parent.SelectedImageIndex = 0;
                this.tnActivatedChar = tn;
                this.eaConfig.ActivatedChar = (EveCharacter) tn.Tag;
                this.eaConfig.ActivatedAccount = (EveAccount) tn.Parent.Tag;
            }
        }

        private void AddAccount(EveAccount ea)
        {
            TreeNode tn = new TreeNode(ea.Name);
            tn.Tag = ea;
            tn.ImageIndex = 1;
            tn.SelectedImageIndex = 1;
            this.tvCharacter.Nodes.Add(tn);
            for (int i = 0; i < ea.CharacterCount; i++)
            {
                EveCharacter character = ea.GetCharacter(i);
                string name = character.Name;
                string corpName = character.CorpName;
                if (name == "")
                {
                    name = "<Not Set>";
                }
                if (corpName == "")
                {
                    corpName = "<Not Set>";
                }
                TreeNode node2 = new TreeNode(corpName);
                node2.ImageIndex = 2;
                node2.SelectedImageIndex = 2;
                TreeNode node3 = new TreeNode(name);
                node3.ImageIndex = 1;
                node3.SelectedImageIndex = 1;
                node2.Tag = "Corp";
                node3.Tag = character;
                tn.Nodes.Add(node3);
                node3.Nodes.Add(node2);
                if ((this.eaConfig.ActivatedChar != null) && (this.eaConfig.ActivatedChar.CharID == character.CharID))
                {
                    this.ActivateCharacter(node3);
                }
            }
        }

        private void tvCharacter_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = this.tvCharacter.SelectedNode;
            if (selectedNode.Tag is EveCharacter)
            {
                this.ActivateCharacter(selectedNode);
            }
            else if (selectedNode.Tag.Equals("Corp"))
            {
                this.ActivateCharacter(selectedNode.Parent);
            }
        }

        private void cmCharacter_Popup(object sender, EventArgs e)
        {
            TreeNode selectedNode = this.tvCharacter.SelectedNode;
            if ((selectedNode.Tag is EveCharacter) || selectedNode.Tag.Equals("Corp"))
            {
                this.cmCharacter.MenuItems[0].Enabled = true;
            }
            else
            {
                this.cmCharacter.MenuItems[0].Enabled = false;
            }
        }

        private void InitImageList()
        {
            if (Screen.PrimaryScreen.Bounds.Width > 320)
            {
                this.ilCharacter.ImageSize = new Size(0x20, 0x20);
                this.ilCharacter.Images.Add((Icon) EVEResources.Checked_L);
                this.ilCharacter.Images.Add((Icon) EVEResources.NotChecked_L);
                this.ilCharacter.Images.Add((Icon) EVEResources.Info_L);
            }
            else
            {
                this.ilCharacter.Images.Add(EVEResources.Checked);
                this.ilCharacter.Images.Add(EVEResources.NotChecked);
                this.ilCharacter.Images.Add(EVEResources.Info);
            }
        }

        private void onExit(object sender, EventArgs e)
        {
            this.eaConfig.SaveToFile(this.eaConfig.Filename);
            base.DialogResult = DialogResult.OK;
            base.Close();
        }

        private void onSelectClick(object sender, EventArgs e)
        {
            TreeNode selectedNode = this.tvCharacter.SelectedNode;
            if ((selectedNode.Tag != null) && (selectedNode.Tag is EveCharacter))
            {
                this.tvCharacter.BeginUpdate();
                this.ActivateCharacter(selectedNode);
                this.tvCharacter.EndUpdate();
            }
            else if (selectedNode.Tag.Equals("Corp"))
            {
                this.tvCharacter.BeginUpdate();
                this.ActivateCharacter(selectedNode.Parent);
                this.tvCharacter.EndUpdate();
            }
        }

        private void onUpdate(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            EveCharacterList list = new EveCharacterList();
            foreach (KeyValuePair<string, EveAccount> pair in this.eaConfig.Accounts)
            {
                if (!list.Load(pair.Value))
                {
                    continue;
                }
                pair.Value.DelAllCharacters();
                foreach (EveCharacter character in list.Chars)
                {
                    pair.Value.AddCharacter(character);
                }
            }
            this.eaConfig.ActivatedAccount = null;
            this.eaConfig.ActivatedChar = null;
            this.UpdateCharacterTree();
            Cursor.Current = Cursors.Default;
        }

        private void UpdateCharacterTree()
        {
            this.tvCharacter.BeginUpdate();
            this.tvCharacter.Nodes.Clear();
            this.tnActivatedChar = null;
            if (this.eaConfig.ActivatedAccount == null)
            {
                if (this.eaConfig.Accounts.Count > 0)
                {
                    foreach (EveAccount account in this.eaConfig.Accounts.Values)
                    {
                        if (account.CharacterCount > 0L)
                        {
                            this.eaConfig.ActivatedAccount = account;
                            this.eaConfig.ActivatedChar = this.eaConfig.ActivatedAccount.GetCharacter(0);
                            break;
                        }
                    }
                }
                if (this.eaConfig.ActivatedAccount == null)
                {
                    this.eaConfig.ActivatedAccount = this.eaConfig.Accounts.GetEnumerator().Current.Value;
                }
            }
            foreach (KeyValuePair<string, EveAccount> pair in this.eaConfig.Accounts)
            {
                this.AddAccount(pair.Value);
            }
            this.tvCharacter.ExpandAll();
            this.tvCharacter.EndUpdate();
        }
    }
}